package com.tangtang.gulimall.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tangtang.gulimall.product.Vo.BrandVo;
import com.tangtang.gulimall.product.entity.BrandEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
//import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.tangtang.gulimall.product.entity.CategoryBrandRelationEntity;
import com.tangtang.gulimall.product.service.CategoryBrandRelationService;
import com.tangtang.common.utils.PageUtils;
import com.tangtang.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author tangtang
 * @email a1104707271@vip.qq.com
 * @date 2020-09-11 17:25:31
 */
@RestController
@RequestMapping("product/categorybrandrelation")
@Slf4j
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 商品中的品牌关联,需要返回一个品牌id和一个品牌名称
     */
    @GetMapping("/brands/list")
    public R brandslist(@RequestParam(value = "catId",required = true) Long catId)
    {
        //System.out.println("获取参数："+catId);
        //前期测试参数选择书刊的分类列表
        List<BrandEntity> brandEntities = categoryBrandRelationService.getbrandid(catId);

        //使用Vo进行返回
        List<BrandVo> brandVoList = new ArrayList<BrandVo>();
        for (BrandEntity brandEntity:brandEntities)
        {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(brandEntity.getBrandId());
            brandVo.setBrandName(brandEntity.getName());
            brandVoList.add(brandVo);
        }
        return R.ok().put("data",brandVoList);
    }
    /**
     * 品牌关联的数据列表便已经完成了
     */
    @RequestMapping("/catelog/list")
    public R categorylist(@RequestParam("brandId") Long brandId){
        //System.out.println("有请求进入相应的数据处理");
        //PageUtils page = categoryBrandRelationService.queryPage(params);
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<CategoryBrandRelationEntity>();
        if (!StringUtils.isEmpty(brandId)){
            queryWrapper.eq("brand_id",brandId);
        }
        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(queryWrapper);
        return R.ok().put("data",list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     * 逆向生成的参数使用的是一个实体类
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")，
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        //原来的保存,不存储名称等
        //categoryBrandRelationService.save(categoryBrandRelation);
        //根据传来的id进行中间表的关联查询
		categoryBrandRelationService.saveall(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
